package com.mzy.xmyx.acl.controller;

import com.mzy.xmyx.common.result.Result;
import com.mzy.xmyx.acl.service.PermissionService;
import com.mzy.xmyx.model.acl.Permission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "菜单管理")
@RestController
@RequestMapping("/admin/acl/permission")
@CrossOrigin
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @ApiOperation("查询所有菜单")
    @GetMapping
    public Result list() {
        List<Permission> permissionList = permissionService.queryAllPermission();
        return Result.ok(permissionList);
    }

    @ApiOperation("添加菜单")
    @PostMapping("/save")
    public Result save(@RequestBody Permission permission) {
        boolean isSuccess = permissionService.save(permission);
        return Result.ok(isSuccess);
    }

    @ApiOperation("更新菜单")
    @PutMapping("/update")
    public Result update(@RequestBody Permission permission) {
        boolean isSuccess = permissionService.updateById(permission);
        return Result.ok(isSuccess);
    }

    @ApiOperation("递归删除菜单")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Long id) {
        boolean isSuccess = permissionService.removeChildById(id);
        return Result.ok(isSuccess);
    }

    @ApiOperation("获取已分配的权限和所有权限")
    @GetMapping("/toAssign/{roleId}")
    public Result toAssign(@PathVariable Long roleId) {
        // 获取权限列表（isSelect 标记为 true 的是已选中的）
        List<Permission> allPermissions = permissionService.getPermissionByRoleId(roleId);

        return Result.ok(allPermissions);
    }

    @ApiOperation("给某个角色授权")
    @GetMapping("/doAssign")
    public Result doAssign(@RequestParam("roleId") Long roleId,
                           @RequestParam("permissionId") List<Long> permissionIdList) {
        Boolean isSuccess = permissionService.saveRolePermissionRelationship(roleId, permissionIdList);
        return Result.ok(isSuccess);
    }


}
